package com.boc.langxiang.ui.fragment.index;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.slip.SwipeViewPager;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.index.IndexContract;
import com.boc.langxiang.entity.response.ArticleEntity;
import com.boc.langxiang.entity.response.IndexAdmEntity;
import com.boc.langxiang.entity.response.IndexArticleContent;
import com.boc.langxiang.entity.response.IndexBannerEntity;
import com.boc.langxiang.presenter.index.IndexFragmentPresenter;
import com.boc.langxiang.ui.activity.index.ArticleDetailActivity;
import com.boc.langxiang.ui.activity.index.HotArticleActivity;
import com.boc.langxiang.ui.adapter.index.IndexAdapter;
import com.boc.langxiang.ui.adapter.index.IndexBannerPageAdapter;
import com.boc.langxiang.ui.adapter.index.IndexCoverPagerAdapter;
import com.boc.langxiang.ui.fragment.BaseFragment;
import com.boc.langxiang.util.T;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.crosswall.lib.coverflow.CoverFlow;
import me.crosswall.lib.coverflow.core.PagerContainer;

/**
 * 首页
 */
public class IndexFragment extends BaseFragment implements IndexContract.View,
        SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {

    @BindView(R.id.recylerview)
    RecyclerView mRecylerview;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    private IndexContract.Presenter mPresenter;
    private ArrayList<ArticleEntity> mArticles;
    private IndexAdapter mIndexAdapter;
    private SwipeViewPager mBannerViewpager;
    private PagerContainer mPagerContainer;
    private TextView mSeeMore;
    private boolean mIsOnRefresh;
    private IndexBannerPageAdapter mBannerPageAdapter;
    private ArrayList<IndexBannerEntity> mBanners;
    private ArrayList<IndexAdmEntity> mAdms;
    private IndexCoverPagerAdapter mCoverPagerAdapter;

    public static IndexFragment newInstance() {
        IndexFragment fragment = new IndexFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_index, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPresenter = new IndexFragmentPresenter(this);

        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
        mRecylerview.setHasFixedSize(true);
        mRecylerview.setLayoutManager(new LinearLayoutManager(mContext));

        initAdapter();
        addHeadView();

        getFlaglist();
    }

    private void getFlaglist() {
        mPresenter.getFlaglist();
    }

    private void initAdapter() {
        mArticles = new ArrayList<>();
        mIndexAdapter = new IndexAdapter(mContext, mArticles);
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.view_empty, (ViewGroup) mRecylerview.getParent(), false);
        mIndexAdapter.setEmptyView(emptyView);
        mIndexAdapter.openLoadAnimation();
        mIndexAdapter.isFirstOnly(false);
//        mIndexAdapter.setOnLoadMoreListener(this);
//        mIndexAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mRecylerview.setAdapter(mIndexAdapter);
        mIndexAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(mContext, ArticleDetailActivity.class);
                ArticleEntity item = mIndexAdapter.getItem(position);
                intent.putExtra("data", item);
                startActivity(intent);
            }
        });
    }

    private void addHeadView() {
        View headView = LayoutInflater.from(mContext).inflate(R.layout.header_index, null, false);
        mBannerViewpager = (SwipeViewPager) headView.findViewById(R.id.banner_viewpager);
        mBanners = new ArrayList<>();
        mBannerPageAdapter = new IndexBannerPageAdapter(getActivity(), mBanners);
        mBannerViewpager.setAdapter(mBannerPageAdapter);
        mBannerViewpager.startScorll();

        mPagerContainer = (PagerContainer) headView.findViewById(R.id.pager_container);
        ViewPager pager = mPagerContainer.getViewPager();
        mAdms = new ArrayList<>();
        mCoverPagerAdapter = new IndexCoverPagerAdapter(mContext, mAdms);
        pager.setAdapter(mCoverPagerAdapter);
        pager.setClipChildren(false);
        pager.setOffscreenPageLimit(6);
        new CoverFlow.Builder()
                .with(pager)
                .scale(0.2f)
                .pagerMargin(getResources().getDimensionPixelSize(R.dimen.pager_margin))
                .spaceSize(0f)
                .build();


        mSeeMore = (TextView) headView.findViewById(R.id.see_more);
        mSeeMore.setOnClickListener(this);


        mIndexAdapter.addHeaderView(headView);
    }

    @Override
    public void showLoading() {
        showProgressDialog();
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        mIsOnRefresh = false;
        mSwipeLayout.setRefreshing(false);
        T.showShort(mContext, msg);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onRefresh() {
        if (mIsOnRefresh) {
            return;
        }
        mIsOnRefresh = true;
        mPresenter.getFlaglist();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.see_more:
                startActivity(new Intent(mContext, HotArticleActivity.class));
                break;
        }
    }

    @Override
    public void getFlaglistSuccess(IndexArticleContent content) {
        if (!isAdded()) {
            return;
        }
        mIsOnRefresh = false;
        mSwipeLayout.setRefreshing(false);
        if (content == null) {
            return;
        }
        ArrayList<ArticleEntity> getFlaglist = content.getGetFlaglist();
        if (getFlaglist != null) {
            mArticles.clear();
            mArticles.addAll(getFlaglist);
            mIndexAdapter.setNewData(mArticles);
        }

        ArrayList<IndexBannerEntity> topphotolist = content.getTopphotolist();
        if (topphotolist != null) {
            mBanners.clear();
            mBanners.addAll(topphotolist);
            mBannerPageAdapter.notifyDataSetChanged();
        }


        ArrayList<IndexAdmEntity> admlist = content.getAdmlist();
        if (admlist != null) {
            mAdms.clear();
            mAdms.addAll(admlist);
            mCoverPagerAdapter.notifyDataSetChanged();
            if (admlist.size() > 2) {
                mPagerContainer.getViewPager().setCurrentItem(1);
            }
        }

    }
}